/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.caselogdocument.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.enums.ViewPageEnum;
import com.icinfo.cloud.provider.punish.caselogdocument.dto.CaseIogDocumentViewDto;
import com.icinfo.cloud.provider.punish.caselogdocument.dto.CaseIogDocumentViewPageQueryRequest;
import com.icinfo.cloud.provider.punish.caselogdocument.mapper.CaseIogDocumentViewMapper;
import com.icinfo.cloud.provider.punish.caselogdocument.model.CaseIogDocumentView;
import com.icinfo.cloud.provider.punish.caselogdocument.service.ICaseIogDocumentViewService;
import com.icinfo.cloud.provider.punish.caselogdocument.vo.CaseIogDocumentViewVo;
import com.icinfo.cloud.provider.punish.csource.manage.mapper.CaseSourceRegisterMainInfoMapper;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceRegisterMainInfo;
import com.icinfo.cloud.provider.punish.csource.manage.service.ICaseSourcePartyInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseDocumentPageDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICasePartiesRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 描述: 案件文书浏览日志表 case_log_document_view 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2023年02月21日
 */
@Service
public class CaseIogDocumentViewServiceImpl extends MyBatisServiceSupport implements ICaseIogDocumentViewService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseIogDocumentViewServiceImpl.class);

    @Resource
    private CaseIogDocumentViewMapper caseIogDocumentViewMapper;

    @Resource
    private ICasePartiesRecordService casePartiesRecordService;

    @Resource
    private ICaseMainInfoService caseMainInfoService;
    @Resource
    private CaseSourceRegisterMainInfoMapper caseSourceRegisterMainInfoMapper;
    @Resource
    private ICaseSourcePartyInfoService caseSourcePartyInfoService;
    @Resource
    private CaseMainInfoMapper caseMainInfoMapper;

    @Override
    public PageResponse<CaseIogDocumentViewVo> getPageList(CaseIogDocumentViewPageQueryRequest request) {
        this.checkOrgCode(request);
        PageHelper.startPage(request.getPageNum(), request.getLength());
        List<CaseIogDocumentViewVo> list = caseIogDocumentViewMapper.queryPageList(request);
        for (CaseIogDocumentViewVo vo:list) {
            vo.setViewPage(ViewPageEnum.getValueByCode(vo.getViewPage()));

        }
        return new PageResponse<>(list);
    }
    private void checkOrgCode(CaseIogDocumentViewPageQueryRequest request) {
        request.setOrg(0);
        if (StrUtil.isBlank(request.getCaseOrgCode())) {
            return;
        }
        List<String> orgCodeList = new ArrayList<>();
        List<String> deptCodeList = new ArrayList<>();
        String[] str = request.getCaseOrgCode().split(",");

        for (String s : str) {
            if (s.indexOf("_") > 0) {
                orgCodeList.add(s);
            } else {
                deptCodeList.add(s);
            }
        }
        if (orgCodeList.size() > 0 && deptCodeList.size() > 0) {
            request.setOrg(1);
        } else if (orgCodeList.size() > 0) {
            request.setOrg(2);
        } else if (deptCodeList.size() > 0) {
            request.setOrg(3);
        }

        request.setOrgCodeList(orgCodeList);
        request.setDeptCodeList(deptCodeList);
    }

    @Override
    public int addCaseIogDocument(CaseIogDocumentViewDto caseIogDocumentViewDto) {
        int num=0;
        if(Objects.isNull(caseIogDocumentViewDto.getCaseId())){
            logger.info("caseId为空，直通车文书未关联案件的查询，无需记录日志");
            return -1;
        }
        CaseMainInfo mainInfo = caseMainInfoMapper.selectByPrimaryKey(caseIogDocumentViewDto.getCaseId());
        CaseSourceRegisterMainInfo caseSourceRegisterMainInfo=null;
        if(!ObjectUtils.isEmpty(mainInfo)){
            num=saveCaseLogDocument(mainInfo,caseIogDocumentViewDto);
        }else {
            caseSourceRegisterMainInfo=caseSourceRegisterMainInfoMapper.selectByPrimaryKey(caseIogDocumentViewDto.getCaseId());
            if(!ObjectUtils.isEmpty(caseSourceRegisterMainInfo)){
                num=saveCaseSourceLogDocument(caseSourceRegisterMainInfo,caseIogDocumentViewDto);
            }
        }
        return num;
    }
    /**
     * 保存案源文书
     * @author liyafeng
     * @date 2023/2/24
      * @param caseIogDocumentViewDto
     * @return
     */
    private int saveCaseSourceLogDocument(CaseSourceRegisterMainInfo caseSourceRegisterMainInfo,CaseIogDocumentViewDto caseIogDocumentViewDto) {
        CaseIogDocumentView bean = new CaseIogDocumentView();
        BeanUtil.copyProperties(caseIogDocumentViewDto,bean);
        String uuid = StringUtils.uuid();
        bean.setId(uuid);
        String partyName = caseSourcePartyInfoService.getCaseSourcePartyName(caseIogDocumentViewDto.getCaseId());
        UserDTO userDTO = UserUtil.getUserInfo();
        Date nowDate = new Date();
        bean.setPartyName(partyName);
        bean.setCaseName(caseSourceRegisterMainInfo.getCaseSourceName());
        bean.setCaseOrgName(caseSourceRegisterMainInfo.getHandleOrgName());
        bean.setCaseOrgCode(caseSourceRegisterMainInfo.getHandleOrgCode());
        bean.setCaseDeptCode(caseSourceRegisterMainInfo.getHandleDeptCode());
        bean.setCaseDeptName(caseSourceRegisterMainInfo.getHandleDeptName());
        bean.setViewTime(nowDate);
        bean.setAddTime(nowDate);
        String orgName = userDTO.getOrgName();
        String orgCode = userDTO.getOrgCode();
        Long userId = userDTO.getUserId();
        String realName = userDTO.getRealName();
        bean.setViewOrgName(orgName);
        bean.setViewOrgCode(orgCode);
        bean.setViewUserId(userId.toString());
        bean.setViewUserName(realName);
        //浏览文书地址
        int num=caseIogDocumentViewMapper.insert(bean);
        return num;
    }

    /**
     * 保存案件文书浏览信息
     * @author liyafeng
     * @date 2023/2/24
      * @param caseIogDocumentViewDto
     * @return
     */
    private int saveCaseLogDocument(CaseMainInfo mainInfo,CaseIogDocumentViewDto caseIogDocumentViewDto) {
        CaseIogDocumentView bean = new CaseIogDocumentView();
        BeanUtil.copyProperties(caseIogDocumentViewDto,bean);
        String uuid = StringUtils.uuid();
        bean.setId(uuid);
        //根据案件id获取当事人姓名
        String partyName = casePartiesRecordService.getPartyNamesByCaseId(caseIogDocumentViewDto.getCaseId());
        String caseNumber = mainInfo.getCaseNumber();
        String caseName = mainInfo.getCaseName();
        String caseOrgName = mainInfo.getCaseOrgName();
        String caseOrgCode= mainInfo.getCaseOrgCode();
        //获取登录用户信息
        UserDTO userDTO = UserUtil.getUserInfo();
        Date nowDate = new Date();
        bean.setPartyName(partyName);
        bean.setCaseNumber(caseNumber);
        bean.setCaseName(caseName);
        bean.setCaseOrgName(caseOrgName);
        bean.setCaseOrgCode(caseOrgCode);
        bean.setCaseDeptCode(mainInfo.getCaseDeptCode());
        bean.setCaseDeptName(mainInfo.getCaseDeptName());
        bean.setViewTime(nowDate);
        bean.setAddTime(nowDate);
        String orgName = userDTO.getOrgName();
        String orgCode = userDTO.getOrgCode();
        Long userId = userDTO.getUserId();
        String realName = userDTO.getRealName();
        bean.setViewOrgName(orgName);
        bean.setViewOrgCode(orgCode);
        bean.setViewUserId(userId.toString());
        bean.setViewUserName(realName);
        //浏览文书地址
        int num=caseIogDocumentViewMapper.insert(bean);
        return num;
    }

}